package dat255.calendarclock.controller;

import java.util.Observable;
import java.util.Observer;

import dat255.calendarclock.R;
import dat255.calendarclock.model.event.Event;
import dat255.calendarclock.model.event.EventHandler;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class NotificationService extends Service implements Observer{
	String ns = Context.NOTIFICATION_SERVICE;
	private NotificationManager notifManager;
	private static final int APP_ID = 0; 
	
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	@Override
	public void onCreate() {
		EventHandler.getInstance().addObserver(this);
			
	}

	@Override
	public void onDestroy() {
		notifManager.cancel(R.string.app_name);
	}
	
	@Override
	public void onStart(Intent intent, int startid) {
		notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}
	

	@Override
	public void update(Observable observable, Object data) {
		if(data instanceof Event) {
			Intent intent = new Intent(this,NotificationService.class);
			Notification notification = new Notification(R.drawable.icon,
					((Event) data).getName(), System.currentTimeMillis());

			notification.setLatestEventInfo(NotificationService.this,
					"CalendarCLock",((Event) data).getName()+"\n"+((Event) data).getName(),
					PendingIntent.getService(this.getBaseContext(), 0, intent,
							PendingIntent.FLAG_CANCEL_CURRENT));

			notifManager.notify(APP_ID, notification);
		}
	}

	
}
